package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.article.dto.ArticleDto;
import com.heima.article.dto.ArticleHomeDto;
import com.heima.article.dto.ArticleInfoDto;
import com.heima.article.entity.ApArticle;
import com.heima.article.entity.ApArticleContent;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.IApArticleContentService;
import com.heima.article.service.IApArticleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.service.IPublishService;
import com.heima.common.dto.ResponseResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 文章信息表，存储已发布的文章 服务实现类
 * </p>
 *
 * @author gq
 * @since 2022-06-24
 */
@Service
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements IApArticleService {
    @Autowired
    IApArticleContentService iApArticleContentService;
    @Autowired
    @Lazy
    private IPublishService iPublishService;

    /**
     * 保存文章
     *
     * @param articleDto
     * @return
     */
    @Override
    public ResponseResult<Long> saveArticle(ArticleDto articleDto) {
        ApArticle article = new ApArticle();
        BeanUtils.copyProperties(articleDto, article);
//    判断是否更新
        if (articleDto.getId() == null) {
//          添加创建时间
            article.setCreatedTime(new Date());
//          保存文章
            this.save(article);
//          保存文章内容
            ApArticleContent content = new ApArticleContent();
            content.setArticleId(article.getId());
            content.setContent(articleDto.getContent());
            iApArticleContentService.save(content);

//            生成静态HTML页面,使用线程池完成
            iPublishService.createHTML(article,articleDto.getContent());

//            返回新增文章的ID
            return ResponseResult.okResult(article.getId());
        }
//    更新文章
        this.saveOrUpdate(article);
//    更新文章内容
        LambdaUpdateWrapper<ApArticleContent> updateWrapper = new LambdaUpdateWrapper();
        updateWrapper.eq(ApArticleContent::getArticleId, articleDto.getId());
        updateWrapper.set(ApArticleContent::getContent, articleDto.getContent());
        iApArticleContentService.update(updateWrapper);
//        TODO 更新生成HTML页面
//            iPublishService.
        return ResponseResult.okResult(articleDto.getId());
    }

    /**
     * 加载文章
     *
     * @param dto
     * @param type 1:最新  0:更多
     * @return
     */
    @Override
    public ResponseResult load(ArticleHomeDto dto, int type) {
        // 分页查询
        LambdaQueryWrapper<ApArticle> query = new LambdaQueryWrapper<>();
        // 判断频道
        if (dto.getChannelId() != 0) {
            query.eq(ApArticle::getChannelId, dto.getChannelId());
        }
        // 如果是已下架或者标明已删除的文章则不被查询出来
        query.eq(ApArticle::getIsDelete, false);
        query.eq(ApArticle::getIsDown, false);
        if (type == 0) {
            // 如果是加载更多,查询比前端传递的最小时间还早的文章
            query.lt(ApArticle::getPublishTime, dto.getMinTime());
        } else {
            // 如果是加载最新,查询比前端传递的最大时间还晚的文章
            query.gt(ApArticle::getPublishTime, dto.getMaxTime());
        }
        // 按照发布时间倒序排
        query.orderByDesc(ApArticle::getPublishTime);
        IPage<ApArticle> page = new Page<>(1, dto.getSize());

        IPage<ApArticle> iPage = this.page(page, query);
        // 只需要给前端返回数据
        return ResponseResult.okResult(iPage.getRecords());
    }

    @Override
    public ResponseResult loadInfo(ArticleInfoDto dto) {
        // 根据文章id查询文章和文章内容
        ApArticle apArticle = this.getById(dto.getArticleId());
        // 根据文章id查询内容
        Wrapper<ApArticleContent> query = new LambdaQueryWrapper<>();
        ((LambdaQueryWrapper<ApArticleContent>) query).eq(ApArticleContent::getArticleId, dto.getArticleId());
        ApArticleContent articleContent = iApArticleContentService.getOne(query);
        // 定义返回格式
        Map<String, Object> map = new HashMap<>();
        map.put("config", apArticle);
        map.put("content", articleContent);
        return ResponseResult.okResult(map);
    }
}
